local tuxi = fk.CreateSkill{
  name = "iberia__tuxi",
}

Fk:loadTranslationTable{
  [tuxi.name] = "突袭",
  [":"..tuxi.name] = "摸牌阶段，你可以少摸任意张牌获得等量名其他角色各一张手牌。",

  [tuxi.name.."-invoke"] = "突袭：可以少摸任意张牌获得等量名其他角色各一张手牌",
}

tuxi:addEffect(fk.DrawNCards, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(tuxi.name)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getAlivePlayers(), function (p)
      return p ~= player and not p:isKongcheng()
    end)
    local tos = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 0,
      max_num = data.n,
      skill_name = tuxi.name,
      prompt = tuxi.name.."-invoke",
    })

    if #tos == 0 then return end
    event:setCostData(self, tos)
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self)
    data.n = data.n - #tos
    for _, p in ipairs(tos) do
      if player.dead then break end
      if not p:isKongcheng() then
        room:obtainCard(player, room:askToChooseCard(player, {
          target=  p,
          flag = "h",
          min_num = 1,
          max_num = 1,
          skill_name = tuxi.name,
        }), false, fk.ReasonPrey, player, tuxi.name)
      end
    end
  end
})

tuxi:addAI({
  think = function (self, ai)
    local player = ai.player
    local targets = table.filter(ai:getEnabledTargets(), function (p)
      return p ~= player and not p:isKongcheng()
    end)
    local benefits = table.map(targets, function(p)
      return { p, ai:getBenefitOfEvents(function(logic)
        local c = p:getCardIds("h")[1]
        logic:obtainCard(player, c, false, fk.ReasonPrey)
      end) - ai:getBenefitOfEvents(function (logic)
        logic:drawCards(player, 1)
      end)}
    end)

    local total, tos = 0, {}
    table.sort(benefits, function(a, b) return a[2] > b[2] end)
    for i, benefit in ipairs(benefits) do
      local p, v = table.unpack(benefit)
      if v < 0 then break end
      total = total + v
      table.insert(tos, p)
      if i == 2 then break end
    end

    if #tos == 0 or total <= 0 then return "" end
    return { targets = tos }, total
  end,
})

return tuxi